/*
 * @Author: bankeyu-dell 573569790@qq.com
 * @Date: 2022-10-12 21:31:57
 * @LastEditors: bankeyu-dell 573569790@qq.com
 * @LastEditTime: 2022-10-13 22:05:59
 * @FilePath: \fast-vue3\src\store\modules\customer\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from 'pinia';
import { passLogon as customerLogin, logout as customerLogout, PassLoginData } from '/@/api/customer/index';
import { setToken, clearToken } from '/@/utils/auth';
import { CustomerState } from './types';

export const useCustomerStore = defineStore('customer', {
  state: (): CustomerState => ({
    cardId: undefined,
    cardName: undefined,
    cardType: undefined,
    customerName: undefined,
    customerNo: undefined,
    gender: undefined,
    id: undefined,
    levelNo: undefined,
    email: undefined,
    customerRels: [],
    mobile: undefined,
    namespace: undefined,
    portrait: undefined,
    room: undefined,
    sessionid: undefined,
    status: undefined,
  }),
  getters: {
    customerProfile(state: CustomerState): CustomerState {
      return { ...state };
    },
  },
  actions: {
    // 设置用户的信息
    setInfo(partial: Partial<CustomerState>) {
      this.$patch(partial);
    },
    // 重置用户信息
    resetInfo() {
      this.$reset();
    },
    // 异步登录并存储token
    async login(loginForm: PassLoginData) {
      const result = await customerLogin(loginForm);
      const token = result?.token;
      if (token) {
        setToken(token);
      }
      return result;
    },
    // Logout
    async logout() {
      await customerLogout();
      this.resetInfo();
      clearToken();
      // 路由表重置
      // location.reload();
    },
  },
});
